/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bluelinelabs.conductor.demo.slice;

import com.bluelinelabs.conductor.conductor.Conductor;
import com.bluelinelabs.conductor.conductor.Router;
import com.bluelinelabs.conductor.conductor.RouterTransaction;
import com.bluelinelabs.conductor.conductor.manager.ObjectManager;
import com.bluelinelabs.conductor.demo.ResourceTable;
import com.bluelinelabs.conductor.demo.controllers.HomeController;
import com.bluelinelabs.conductor.demo.util.CallBackManager;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.window.service.DisplayManager;
import ohos.bundle.AbilityInfo;
import ohos.multimodalinput.event.KeyEvent;


/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class MainAbilitySlice extends AbilitySlice {
    private Router router;
    private int cur_rotation;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        cur_rotation = DisplayManager.getInstance().getDefaultDisplay(this).get().getRotation();

        getWindow().setStatusBarColor(Color.getIntColor("#2A3334"));

        ComponentContainer Id_controller_container = (ComponentContainer) findComponentById(ResourceTable.Id_controller_container);
        router = Conductor.attachRouter(this, Id_controller_container, intent.getParams());
        if (!router.hasRootController()) {
            router.setRoot(RouterTransaction.with(new HomeController()));
        }
        ObjectManager.getInstance().setToolbar((Text) findComponentById(ResourceTable.Id_title_bar));
        ObjectManager.getInstance().setIdTitleAbout((Text) findComponentById(ResourceTable.Id_title_about));
    }


    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
        cur_rotation = DisplayManager.getInstance().getDefaultDisplay(this).get().getRotation();
        CallBackManager.notifyScreenOrirentaionChange();
    }

    public int getDisplayOrientation() {
        return cur_rotation;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackPressed() {
        if (!router.handleBack()) {
            super.onBackPressed();
        }
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        router.onActivityResult(requestCode, resultCode, resultData);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
        return super.onKeyDown(keyCode, keyEvent);


    }
}
